package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
@Service
@Transactional
public class CourierServiceImpl implements CourierService {

	@Autowired
	private CourierRepository courierRepository;
	
	//保存添加的快递员
	@Override
	public void saveCourier(Courier courier) {
		courierRepository.save(courier);
	}
	
	//查询所有的快递员信息
	@Override
	public Page<Courier> findAll(Specification<Courier> specification, Pageable pageable) {
		Page<Courier> pageData = courierRepository.findAll(specification,pageable);
		return pageData;
	}

	@Override
	public void delBatch(String[] idArray) {
		//循环IDarray得到每一个id,再调用dao层修改
		for (String idStr : idArray) {
			int id = Integer.parseInt(idStr);
			courierRepository.updateDelTag(id);
		}
		
	}

	@Override
	public List<Courier> findNoAssociation() {
		//封装查询条件:fixedAreas为空的快递员
		Specification<Courier> specification=new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return predicate;
			}
		};
		List<Courier> couriers=courierRepository.findAll(specification);
		return couriers;
	}

}
